package com.exmaple.result;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

@ControllerAdvice
@RestController
public class GlobalExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = BaseException.class)
    public Result handle(BaseException e) {
        logger.error("BaseException: {}", e.getMessage());
        return Result.error(e.getErrorCode());
    }

    @ExceptionHandler(value = RuntimeException.class)
    public Result handle(RuntimeException e) {
        logger.error("RuntimeException: {}", e.getMessage());
        return Result.error(ErrorCode.SYSTEM_ERROR_B0001);
    }

    @ExceptionHandler(value = Exception.class)
    public Result handle(Exception e) {
        logger.error("Exception: {}", e.getMessage());
        return Result.error(ErrorCode.SYSTEM_ERROR_B0001);
    }
}
